import re
import pandas as pd
from datetime import datetime

def cleaningData(log):
    # TODO：人脸识别，带act的 pattern，无法统计失败次数（后续需改进）
    pattern = r'(\d{2})-(\d{2})(\d{2}:\d{2}:\d{2}\.\d{3}).*face\s*ret\[(0x[0-9a-fA-F]+)\](?:, act:\[(\d+)\]).*'
    logs = []

    for line in log.split('\n'): 
        match = re.search(pattern, line.strip())
        
        if match:
            month, day, time_str, ret_code, act = match.groups()
            timestamp = datetime(datetime.now().year, int(month), int(day), *map(int, time_str.split(':')[0:2]),
                                int(time_str.split(':')[2].split('.')[0]), 
                                int(time_str.split('.')[1])).strftime("%H:%M:%S")
            
            log_entry = {
                'timestamp': timestamp,
                'ret_code': ret_code.lower(),  # 统一转为小写
                'act_phase': int(act)
            }

            logs.append(log_entry)
            # print("log_entry: ", log_entry)
    
    return pd.DataFrame(logs)